# encoding='utf-8'
import sql # 自己写的库
import re

import tkinter
from tkinter import ttk
from tkinter import messagebox
from tkinter import scrolledtext
from tkinter import *

class studentApprise:
	def __init__(self,lst_mainMessage):
		self.studentName=lst_mainMessage[0]
		self.courseTeacher=lst_mainMessage[1]
		self.courseName=lst_mainMessage[2]
		self.proxyTeacher=lst_mainMessage[3]
		self.courseDate=lst_mainMessage[4]
		self.courseWeek=lst_mainMessage[5]
		self.lst_question=[]

		self.top=tkinter.Toplevel()
		self.top.title('问卷调查') # 学生名字
		self.top.geometry('960x1005+950+0')
		self.StudentMessage()
		self.appriseMessage_frame()
		self.top.mainloop()
	'''基本数据'''

	def StudentMessage(self):
		# 姓名标签
		x_studentName=10
		y_stduentName=10
		label_student=Label(self.top,text='学生姓名：')
		label_student.place(x=x_studentName,y=y_stduentName)
		label_studentName=Label(self.top,text=self.studentName,fg='green')
		label_studentName.place(x=x_studentName+60,y=y_stduentName)

		# 上课日期
		x_courseDate=10
		y_courseDate=30
		label_date=Label(self.top,text='上课日期：')
		label_date.place(x=x_courseDate,y=y_courseDate)
		label_courseDate=Label(self.top,text=f'{self.courseDate}-{self.courseWeek}',fg='green')
		label_courseDate.place(x=x_courseDate+60,y=y_courseDate)

		# 上课课程
		x_courseName=10
		y_courseName=50
		label_course=Label(self.top,text='上课课程：')
		label_course.place(x=x_courseName,y=y_courseName)
		label_courseName=Label(self.top,text=self.courseName,fg='green')
		label_courseName.place(x=x_courseName+60,y=y_courseName)

		# 任课教师
		x_courseTeacher=10
		y_courseTeacher=70
		label_teacher=Label(self.top,text='任课教师：')
		label_teacher.place(x=x_courseTeacher,y=y_courseTeacher)
		label_courseTeacher=Label(self.top,text=self.courseTeacher,fg='blue')
		label_courseTeacher.place(x=x_courseTeacher+60,y=y_courseTeacher)

		# 代课教师
		x_proxyTeacher=10
		y_proxyTeacher=90
		label_proxy=Label(self.top,text='代课教师：')
		label_proxy.place(x=x_proxyTeacher,y=y_proxyTeacher)
		label_proxyTeacher=Label(self.top,text=self.proxyTeacher,fg='blue')
		label_proxyTeacher.place(x=x_proxyTeacher+60,y=y_proxyTeacher)

		# 出勤状态
		x_AttendanceStatus=200
		y_AttendanceStatus=10
		label_AttendanceStatus=Label(self.top,text='出勤状态：')
		label_AttendanceStatus.place(x=x_AttendanceStatus,y=y_AttendanceStatus)
		self.radioButton_AttendanceStatus=StringVar()
		AttendanceStatus1=Radiobutton(self.top,text="出勤",variable=self.radioButton_AttendanceStatus,value="出勤")
		AttendanceStatus1.place(x=x_AttendanceStatus+60,y=y_AttendanceStatus)
		AttendanceStatus2=Radiobutton(self.top,text="未出勤",variable=self.radioButton_AttendanceStatus,value="未出勤")
		AttendanceStatus2.place(x=x_AttendanceStatus+120,y=y_AttendanceStatus)
		AttendanceStatus3=Radiobutton(self.top,text="补课",variable=self.radioButton_AttendanceStatus,value="补课")
		AttendanceStatus3.place(x=x_AttendanceStatus+190,y=y_AttendanceStatus)

		# 问卷按钮
		x_button=10
		y_button=120
		def topmost1():
			self.frame1.tkraise()
			button1.config(bg='orange',fg='white')
			self.canvas.yview("moveto",0)
		def topmost2():
			self.frame2.tkraise()
			button2.config(bg='orange',fg='white')
			self.canvas.yview("moveto",0)
		def topmost3():
			self.frame3.tkraise()
			button3.config(bg='orange',fg='white')
			self.canvas.yview("moveto",0)
		def topmost4():
			self.frame4.tkraise()
			button4.config(bg='orange',fg='white')
			self.canvas.yview("moveto",0)
		def topmost5():
			self.frame5.tkraise()
			button5.config(bg='orange',fg='white')
			self.canvas.yview("moveto",0)
		def topmost6():
			self.frame6.tkraise()
			button6.config(bg='orange',fg='white')
			self.canvas.yview("moveto",0)
		def topmost7():
			self.frame7.tkraise()
			button7.config(bg='orange',fg='white')
			self.canvas.yview("moveto",0)
		def topmost8():
			self.frame8.tkraise()
			button8.config(bg='orange',fg='white')
			self.canvas.yview("moveto",0)
		def topmost9():
			self.frame9.tkraise()
			button9.config(bg='orange',fg='white')
			self.canvas.yview("moveto",0)
		def savequestion():
			AttendanceStatus=self.radioButton_AttendanceStatus.get()
			if AttendanceStatus in ['']:
				yesno=messagebox.askyesno('提示', '考勤状态未确认，是否默认为出勤')
				if yesno == False:
					return False
				else:
					AttendanceStatus='出勤'
			i=0
			for eachQuestion in self.lst_question:
			# {'总序号':eachQuestion[0],'题目序号':eachQuestion[1],'流程':eachQuestion[2],'能力归类':eachQuestion[3],'题型':eachQuestion[4],'题目':eachQuestion[5],'选择':[],'细节描述':''}
				lst_getOptionValue=[]
				for eachOption in eachQuestion['选择']:
					lst_getOptionValue.append(eachOption.get())
				questionDetail=eachQuestion['细节描述'].get('1.0',END)
				self.lst_question[i]['选择']=lst_getOptionValue
				self.lst_question[i]['细节描述']=questionDetail
				i=i+1
			courseDate_new=re.sub('日.*','',self.courseDate)
			courseDate_new=re.sub('年|月','-',courseDate_new)
			lst_question_new=re.sub('\'','\\\'',str(self.lst_question))
			try:
				sql.inAppriseLog(self.studentName,courseDate_new,self.courseName,self.courseTeacher,self.proxyTeacher,AttendanceStatus,lst_question_new)
				messagebox.showinfo('提示','保存成功')
				self.top.destroy()
			except Exception as e:
				messagebox.showinfo('提示','数据存储错误，请联系管理员\n'+str(e))

		'''获取选择选项及细节描述'''
		button1=Button(self.top,text='上课前',bd=3,width=13,command=topmost1)
		button1.place(x=x_button,y=y_button)
		button2=Button(self.top,text='上课中-打招呼',bd=3,width=13,command=topmost2)
		button2.place(x=x_button+105,y=y_button)
		button3=Button(self.top,text='上课中-引导主题',bd=3,width=13,command=topmost3)
		button3.place(x=x_button+210,y=y_button)
		button4=Button(self.top,text='上课中-分析结构',bd=3,width=13,command=topmost4)
		button4.place(x=x_button+315,y=y_button)
		button5=Button(self.top,text='上课中-设计图',bd=3,width=13,command=topmost5,bg='pink')
		button5.place(x=x_button+420,y=y_button)
		button6=Button(self.top,text='上课中-搭建',bd=3,width=13,command=topmost6)
		button6.place(x=x_button+525,y=y_button)
		button7=Button(self.top,text='上课中-编程',bd=3,width=13,command=topmost7,bg='pink')
		button7.place(x=x_button+630,y=y_button)
		button8=Button(self.top,text='上课中-介绍',bd=3,width=13,command=topmost8)
		button8.place(x=x_button+735,y=y_button)
		button9=Button(self.top,text='下课后',bd=3,width=13,command=topmost9)
		button9.place(x=x_button+840,y=y_button)
		button10=Button(self.top,text='保存课评',bd=3,width=13,command=savequestion)
		button10.place(x=10,y=970)
	'''显示学生基本信息'''

	def buildQustion(self,choiceFrame,repFromSqlByQuestion):
		x_question=10
		y_question=10
		for eachQuestion in repFromSqlByQuestion: # (101, 1, '上课前', '情绪管理', '单选题', '孩子到校心情：', '兴高采烈', '一般', '抗拒上课', '心情不好', '', '', '', '')
			if eachQuestion[4] == '单选题':
				question=f'{str(eachQuestion[1])}、（{eachQuestion[3]}）{eachQuestion[5]}'
				self.lst_question.append({'总序号':eachQuestion[0],'题目序号':eachQuestion[1],'流程':eachQuestion[2],'能力归类':eachQuestion[3],'题型':eachQuestion[4],'题目':eachQuestion[5],'选择':[]})
				label_question=Label(choiceFrame,text=question)
				label_question.place(x=x_question,y=y_question)
				y_question=y_question+30
				self.radioButton_question=StringVar()
				self.lst_question[-1]['选择'].append(self.radioButton_question)
				for i in range(6,14):
					option=eachQuestion[i]
					if option in ['',None]:
						continue
					question_option=Radiobutton(choiceFrame,text=option,variable=self.radioButton_question,value=option,tristatevalue=0)
					question_option.place(x=x_question+20,y=y_question)
					y_question=y_question+30
			elif eachQuestion[4] == '多选题':
				question=f'{str(eachQuestion[1])}、（{eachQuestion[3]}）{eachQuestion[5]}'
				self.lst_question.append({'总序号':eachQuestion[0],'题目序号':eachQuestion[1],'流程':eachQuestion[2],'能力归类':eachQuestion[3],'题型':eachQuestion[4],'题目':eachQuestion[5],'选择':[]})
				label_question=Label(choiceFrame,text=question)
				label_question.place(x=x_question,y=y_question)
				y_question=y_question+30
				courseMessage=sql.getCourseMessage_courseName(self.courseName)[0]
				lst_structure=courseMessage[3].split('|')
				lst_points=courseMessage[4].split('|')

				if question in ['2、（逻辑思维）能否分析以下完整结构：','1、（逻辑思维）基本结构和功能结构是否完整：','4、（解决问题能力）作品是否有以下基本结构及功能结构']:
					for eachStructure in lst_structure:
						self.CheckButton_question=StringVar()
						self.lst_question[-1]['选择'].append(self.CheckButton_question)
						question_option=Checkbutton(choiceFrame,text=eachStructure,variable=self.CheckButton_question,onvalue=eachStructure,tristatevalue=0)
						question_option.place(x=x_question+20,y=y_question)
						y_question=y_question+30

				elif question in ['4、（理解能力）能否清楚以下知识：']:
					for eachPoint in lst_points:
						self.CheckButton_question=StringVar()
						self.lst_question[-1]['选择'].append(self.CheckButton_question)
						question_option=Checkbutton(choiceFrame,text=eachPoint,variable=self.CheckButton_question,onvalue=eachPoint,tristatevalue=0)
						question_option.place(x=x_question+20,y=y_question)
						y_question=y_question+30

			label_detail=Label(choiceFrame,text='细节描述：')
			label_detail.place(x=x_question+20,y=y_question)
			entry_detail=Text(choiceFrame,width=50,height=3)
			entry_detail.place(x=x_question+90,y=y_question)
			self.lst_question[-1]['细节描述']=entry_detail
			y_question=y_question+60
	'''创建题目和选项'''

	def appriseMessage_frame(self):
		def OnMouseWheel(event):
			self.canvas.yview_scroll(int(-1*(event.delta/120)),"units")
			return "break" 
		'''鼠标滚动'''

		self.canvas=Canvas(self.top,width=910,height=800)
		self.canvas.place(x=10,y=160)

		sbar=Scrollbar(self.top)
		sbar.place(x=930,y=160,width=20,height=800)

		self.frame1=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame1.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame1,anchor="nw")
		self.frame1.bind("<MouseWheel>", OnMouseWheel)
		repFromSqlByQuestion1=sql.getQuestion('上课前')
		studentApprise.buildQustion(self,self.frame1,repFromSqlByQuestion1)

		self.frame2=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame2.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame2,anchor="nw")
		self.frame2.bind("<MouseWheel>", OnMouseWheel)
		repFromSqlByQuestion2=sql.getQuestion('上课中-打招呼')
		studentApprise.buildQustion(self,self.frame2,repFromSqlByQuestion2)

		self.frame3=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame3.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame3,anchor="nw")
		self.frame3.bind("<MouseWheel>", OnMouseWheel)
		repFromSqlByQuestion3=sql.getQuestion('上课中-引导主题')
		studentApprise.buildQustion(self,self.frame3,repFromSqlByQuestion3)

		self.frame4=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame4.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame4,anchor="nw")
		self.frame4.bind("<MouseWheel>", OnMouseWheel)
		repFromSqlByQuestion4=sql.getQuestion('上课中-分析结构')
		studentApprise.buildQustion(self,self.frame4,repFromSqlByQuestion4)

		self.frame5=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame5.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame5,anchor="nw")
		self.frame5.bind("<MouseWheel>", OnMouseWheel)
		repFromSqlByQuestion5=sql.getQuestion('上课中-设计图')
		studentApprise.buildQustion(self,self.frame5,repFromSqlByQuestion5)

		self.frame6=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame6.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame6,anchor="nw")
		self.frame6.bind("<MouseWheel>", OnMouseWheel)
		repFromSqlByQuestion6=sql.getQuestion('上课中-搭建')
		studentApprise.buildQustion(self,self.frame6,repFromSqlByQuestion6)

		self.frame7=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame7.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame7,anchor="nw")
		self.frame7.bind("<MouseWheel>", OnMouseWheel)
		repFromSqlByQuestion7=sql.getQuestion('上课中-编程')
		studentApprise.buildQustion(self,self.frame7,repFromSqlByQuestion7)

		self.frame8=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame8.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame8,anchor="nw")
		self.frame8.bind("<MouseWheel>", OnMouseWheel)
		repFromSqlByQuestion8=sql.getQuestion('上课中-介绍')
		studentApprise.buildQustion(self,self.frame8,repFromSqlByQuestion8)

		self.frame9=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame9.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame9,anchor="nw")
		self.frame9.bind("<MouseWheel>", OnMouseWheel)
		repFromSqlByQuestion9=sql.getQuestion('下课后')
		studentApprise.buildQustion(self,self.frame9,repFromSqlByQuestion9)

		self.frame10=Frame(self.canvas,width=910,height=3000,highlightthickness=1,highlightbackground='black')
		self.frame10.place(x=0,y=0)
		self.canvas.create_window((0,0),window=self.frame10,anchor="nw")
		self.frame10.bind("<MouseWheel>", OnMouseWheel)

		self.canvas.configure(yscrollcommand=sbar.set, scrollregion=self.canvas.bbox("all"))
		sbar.config(command=self.canvas.yview)
	'''创建题目框'''

if __name__ == '__main__':
	# 测试
	lst_mainMessage=['xxx', 'xxx', '机械抓手', '无', '2023年3月3日', '周五']
	studentApprise=studentApprise(lst_mainMessage)